package com.lap.analysis.file.handler;

import com.lap.analysis.source.entity.ClassInfo;
import com.lap.analysis.source.entity.FileInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.ServiceLoader;

/**
 * 文件策略上下文
 *
 * @author Shuisheng Lao(劳水生)
 * @version 0.0.1
 */
@Slf4j
public class FileContext {

  public List<FileInfo> execute(List<ClassInfo> dataList) {
    if (CollectionUtils.isEmpty(dataList)) return Collections.emptyList();

    List<FileInfo> result = new LinkedList<>();
    ServiceLoader<AbstractFileHandler> serviceLoader =
        ServiceLoader.load(AbstractFileHandler.class);
    serviceLoader.forEach(
        item -> {
          log.info("文件执行器::{}", item);

          dataList.forEach(classInfo -> result.addAll(item.execute(classInfo)));
        });

    return result;
  }
}
